require 'sketchup.rb'
require File.join(__FILE__, '../amtf_mixin.rb')
require 'json'
# require 'win32ole'
module AMTF
  class AF导出
    include AMTF_mixin
    def 获取转换信息()
      设置常用对象
      信息h=Hash[]

      选择的ents=[]
      if @selection.count == 0
        UI.messagebox "请选择一个 组件 或者 群组 ！"
        return false
      else
        # entities=@entities
        entities=@selection
        entities.each {|e|
          if e.is_a? Sketchup::ComponentInstance or e.is_a? Sketchup::Group
            组件定义名=e.definition.name
            组件实例名=e.name
            组件加实例名=组件定义名+"+"+组件实例名
            puts 组件加实例名
            id=组件加实例名
            信息h[id]=Hash[]
            # puts e.transformation.to_a.to_s
            tr=e.transformation
            str4x4 = tr.to_a.each_slice(4).inject { |str, row| "#{str}\r\n#{row}" }
            puts str4x4
            信息h[id]["transformation"]=str4x4

            point2 = tr.origin
            puts "origin: #{point2.to_s}"
            信息h[id]["原点"]=point2.to_s
            x = tr.xaxis
            puts "xaxis: #{x.to_s}"
            信息h[id]["x轴"]=x.to_s
            y = tr.yaxis
            puts "yaxis: #{y.to_s}"
            信息h[id]["y轴"]=y.to_s

            z = tr.zaxis
            puts "zaxis: #{z.to_s}"
            信息h[id]["z轴"]=z.to_s

            边界大小=获取边界大小(e)
            信息h[id]["x尺寸"]=边界大小[0].to_l
            信息h[id]["y尺寸"]=边界大小[1].to_l
            信息h[id]["z尺寸"]=边界大小[2].to_l
          end
        }
      end

      # puts 信息h.to_a.to_s
      puts 信息h.to_json
      # 信息h.each{|k,v|
      #   angle = vector2.angle_between(v).radians
      #   puts "组件id: #{k}"

      #   v.each{|kk,vv|
      #     单项="#{k}: #{vv}"
      #     angle = vector2.angle_between(v).radians
      #     puts "组件id: #{k}"

      #   }
      # }
    end

    def 导出cad_废移动式
      设置常用对象
      if @selection.count == 0
        UI.messagebox "请先选择一个 拟导出对象 ！"
      else
        @拟导出对象=@selection[0]
      end

      entities=@拟导出对象.definition.entities
      entities.each {|e|
        if ( e.is_a?( Sketchup::ComponentInstance ) || e.is_a?( Sketchup::Group ))
          组件实例名=e.name
          # puts 组件实例名
          if 组件实例名=="_ABF_Label"
            原点=Geom::Point3d.new(0, 0, 0)
            xaxis=Geom::Vector3d.new 1,0,0
            yaxis=Geom::Vector3d.new 0,1,0
            pi=360.degrees

            拟导出对象tr = @拟导出对象.transformation*e.transformation
            标记原点=拟导出对象tr.origin
            puts "标记原点:#{标记原点}"
            # amtf.nil
            # puts defined?(yy)
            vector = 标记原点.vector_to(原点)
            # vector = 原点.vector_to(kk)
            puts "vector:#{vector}"
            tr = Geom::Transformation.new(vector)
            # puts tr
            @拟导出对象.transform! tr
            # amtf.nil

            拟导出对象tr = @拟导出对象.transformation*e.transformation
            标记xaxis=拟导出对象tr.xaxis
            puts "标记xaxis:#{标记xaxis}"
            标记原点=拟导出对象tr.origin
            puts "标记原点:#{标记原点}"
            # kk.nil
            # angle = 标记xaxis.angle_between xaxis
            angle = xaxis.angle_between 标记xaxis
            puts "xaxis.angle_between:#{angle}"
            angle度=angle.radians
            puts "xaxis.angle_between.radians:#{angle度}"
            垂直轴=xaxis*标记xaxis
            # 垂直轴=标记xaxis*xaxis
            # puts "垂直轴:#{垂直轴}"
            # angle垂直轴 = 标记xaxis.angle_between 垂直轴
            # puts "angle垂直轴:#{angle垂直轴.radians}"
            # amtf.nil
            if angle度 < 90
              tr = Geom::Transformation.rotation(原点, 垂直轴, angle)
            else
              tr = Geom::Transformation.rotation(原点, 垂直轴, pi-angle)
            end
            # puts tr
            @拟导出对象.transform! tr
            # amtf.nil

            拟导出对象tr = @拟导出对象.transformation*e.transformation
            标记yaxis=拟导出对象tr.yaxis
            标记原点=拟导出对象tr.origin
            # angle = 标记yaxis.angle_between yaxis
            angle = yaxis.angle_between 标记yaxis
            puts "yangle:#{angle}"
            angle度=angle.radians
            puts "yangle.angle_between.radians:#{angle度}"
            # amtf.nil
            tr = Geom::Transformation.rotation(原点, xaxis, angle)
            # if angle度 < 90
            #   # tr = Geom::Transformation.rotation(原点, xaxis, angle)
            #   tr = Geom::Transformation.rotation(原点, xaxis, pi-angle)
            # else
            #   tr = Geom::Transformation.rotation(原点, xaxis, pi-angle)
            # end
            # puts tr
            @拟导出对象.transform! tr


            # zaxis=Geom::Vector3d.new 0,1,0
            # 拟导出对象tr = @拟导出对象.transformation*e.transformation
            # 标记zaxis=拟导出对象tr.zaxis
            # 标记原点=拟导出对象tr.origin
            # angle = 标记zaxis.angle_between zaxis
            # puts angle
            # tr = Geom::Transformation.new(标记原点,标记zaxis,angle)
            # # puts tr
            # @拟导出对象.transform! tr


          end
        end
      }
    end
    def 导出cad_f2
      设置常用对象
      if @selection.count == 0
        UI.messagebox "请先选择一个 拟导出对象 ！"
      else
        @拟导出对象=@selection[0]
      end

      entities=@拟导出对象.definition.entities
      entities.each {|e|
        if ( e.is_a?( Sketchup::ComponentInstance ) || e.is_a?( Sketchup::Group ))
          组件实例名=e.name
          puts 组件实例名
          if 组件实例名=="_ABF_Label"
            原点=Geom::Point3d.new(0, 0, 0)
            拟导出对象tr = @拟导出对象.transformation*e.transformation
            标记原点=拟导出对象tr.origin
            puts "标记原点:#{标记原点}"
            # puts defined?(yy)
            vector = 标记原点.vector_to(原点)
            # vector = 原点.vector_to(kk)
            puts vector
            tr = Geom::Transformation.new(vector)
            # puts tr
            @拟导出对象.transform! tr

            # kk.nil
            xaxis=Geom::Vector3d.new 1,0,0
            拟导出对象tr = @拟导出对象.transformation*e.transformation
            标记xaxis=拟导出对象tr.xaxis
            puts "标记xaxis:#{标记xaxis}"
            标记原点=拟导出对象tr.origin
            angle = 标记xaxis.angle_between xaxis
            puts "xangle:#{angle}"
            # kk.nil
            垂直轴=xaxis*标记xaxis
            tr = Geom::Transformation.rotation(标记原点, 垂直轴, angle)
            # puts tr
            @拟导出对象.transform! tr
            # kk.nil

            xaxis=Geom::Vector3d.new 1,0,0
            yaxis=Geom::Vector3d.new 0,1,0
            拟导出对象tr = @拟导出对象.transformation*e.transformation
            标记yaxis=拟导出对象tr.yaxis
            标记原点=拟导出对象tr.origin
            angle = 标记yaxis.angle_between yaxis
            puts "yangle:#{angle}"
            tr = Geom::Transformation.rotation(标记原点, xaxis, angle)
            # puts tr
            @拟导出对象.transform! tr


            # zaxis=Geom::Vector3d.new 0,1,0
            # 拟导出对象tr = @拟导出对象.transformation*e.transformation
            # 标记zaxis=拟导出对象tr.zaxis
            # 标记原点=拟导出对象tr.origin
            # angle = 标记zaxis.angle_between zaxis
            # puts angle
            # tr = Geom::Transformation.new(标记原点,标记zaxis,angle)
            # # puts tr
            # @拟导出对象.transform! tr


          end
        end
      }
    end
    def 导出cad
      puts "导出cad"
      设置常用对象
      if @selection.count == 0
        UI.messagebox "请先选择一个 拟导出对象 ！"
      else
        @拟导出对象=@selection[0]
      end
      原点=Geom::Point3d.new(0, 0, 0)
      xaxis=Geom::Vector3d.new 1,0,0
      yaxis=Geom::Vector3d.new 0,1,0
      zaxis=Geom::Vector3d.new 0,0,1

      tr = @拟导出对象.transformation
      x缩放1 = ((Geom::Vector3d.new 1,0,0).transform! tr).length
      y缩放1 = ((Geom::Vector3d.new 0,1,0).transform! tr).length
      z缩放1 = ((Geom::Vector3d.new 0,0,1).transform! tr).length
      puts x缩放1
      puts y缩放1
      puts z缩放1
      边界大小1=获取边界大小(@拟导出对象)
      # amtf.nil
      point1 = Geom::Point3d.new(0, 0, 0)
      x = Geom::Vector3d.new(1, 0, 0)
      y = Geom::Vector3d.new(0, 1, 0)
      z = Geom::Vector3d.new(0, 0, 1)
      tr2 = Geom::Transformation.new(x,y,z,point1)
      @拟导出对象.transformation = tr2
      # # @拟导出对象.transform! tr2
      # tr3 = Geom::Transformation.scaling(x缩放1,y缩放1,z缩放1)
      # @拟导出对象.transform! tr3
      # # kk.nil

      entities=@拟导出对象.definition.entities
      entities.each {|e|
        if ( e.is_a?( Sketchup::ComponentInstance ) || e.is_a?( Sketchup::Group ))
          组件实例名=e.name
          # puts 组件实例名
          if 组件实例名=="_ABF_Label"
            tr = e.transformation
            tr2 = tr.inverse
            # x缩放 = ((Geom::Vector3d.new 1,0,0).transform! tr2).length
            # y缩放 = ((Geom::Vector3d.new 0,1,0).transform! tr2).length
            # z缩放 = ((Geom::Vector3d.new 0,0,1).transform! tr2).length
            # puts x缩放
            # puts y缩放
            # puts z缩放
            @拟导出对象.transformation = tr2
            # $dc_observers.get_class_by_version(@拟导出对象).redraw_with_undo(@拟导出对象)

            # tr4 = Geom::Transformation.scaling(1/x缩放,1/y缩放,1/z缩放)
            # @拟导出对象.transform! tr4
            # tr3 = Geom::Transformation.scaling(x缩放1,y缩放1,z缩放1)
            # @拟导出对象.transform! tr3
            边界大小2=获取边界大小(@拟导出对象)
            # amtf.nil
            # tr = @拟导出对象.transformation
            # x缩放2 = ((Geom::Vector3d.new 1,0,0).transform! tr).length
            # y缩放2 = ((Geom::Vector3d.new 0,1,0).transform! tr).length
            # z缩放2 = ((Geom::Vector3d.new 0,0,1).transform! tr).length
            # puts x缩放2
            # puts y缩放2
            # puts z缩放2
            x缩放=边界大小1[0]/边界大小2[0]
            puts "x缩放:#{x缩放}"
            y缩放=边界大小1[1]/边界大小2[1]
            puts "y缩放:#{y缩放}"
            z缩放=边界大小1[2]/边界大小2[2]
            puts "z缩放:#{z缩放}"
            # tr3 = Geom::Transformation.scaling(x缩放,y缩放,z缩放)

            拟导出对象xaxis=tr2.xaxis
            x_anglex = xaxis.angle_between 拟导出对象xaxis
            puts "拟导出对象xaxis 与 x轴夹角:#{x_anglex}==#{x_anglex.radians}"
            x_平行x轴 = xaxis.parallel?(拟导出对象xaxis)
            puts "x_平行x轴？:#{x_平行x轴}"
            x_angley = yaxis.angle_between 拟导出对象xaxis
            puts "拟导出对象xaxis 与 y轴夹角:#{x_angley}==#{x_angley.radians}"
            x_平行y轴 = yaxis.parallel?(拟导出对象xaxis)
            puts "x_平行y轴？:#{x_平行y轴}"
            x_anglez = zaxis.angle_between 拟导出对象xaxis
            puts "拟导出对象xaxis 与 z轴夹角:#{x_anglez}==#{x_anglez.radians}"
            x_平行z轴 = zaxis.parallel?(拟导出对象xaxis)
            puts "x_平行z轴:#{x_平行z轴}"
            # amtf.nil

            拟导出对象yaxis=tr2.yaxis
            y_angley = yaxis.angle_between 拟导出对象yaxis
            puts "拟导出对象yaxis 与 y轴夹角:#{y_angley}==#{y_angley.radians}"
            y_平行x轴 = yaxis.parallel?(拟导出对象yaxis)
            puts "y_平行x轴？:#{y_平行x轴}"
            y_angley = yaxis.angle_between 拟导出对象yaxis
            puts "拟导出对象yaxis 与 y轴夹角:#{y_angley}==#{y_angley.radians}"
            y_平行y轴 = yaxis.parallel?(拟导出对象yaxis)
            puts "y_平行y轴？:#{y_平行y轴}"
            y_anglez = zaxis.angle_between 拟导出对象yaxis
            puts "拟导出对象yaxis 与 z轴夹角:#{y_anglez}==#{y_anglez.radians}"
            y_平行z轴 = zaxis.parallel?(拟导出对象yaxis)
            puts "y_平行z轴:#{y_平行z轴}"
            # amtf.nil
           if x_平行x轴
            绝对x缩放=x缩放
            if y_平行y轴
              绝对y缩放=y缩放
              绝对z缩放=z缩放
            else
              绝对y缩放=z缩放
              绝对z缩放=y缩放
            end
           elsif x_平行y轴
            绝对y缩放=x缩放
            if y_平行z轴
              绝对x缩放=z缩放
              绝对z缩放=y缩放
            else
              绝对y缩放=x缩放
              绝对z缩放=z缩放
            end
           else #x_平行z轴
            绝对z缩放=x缩放
            if y_平行x轴
              绝对x缩放=y缩放
              绝对y缩放=z缩放
            else
              绝对x缩放=z缩放
              绝对y缩放=y缩放
            end
           end
            # tr3 = Geom::Transformation.scaling(1.968503937007874,1.968503937007874,1.0000000000000004)
            tr3 = Geom::Transformation.scaling(绝对x缩放,绝对y缩放,绝对z缩放)
            @拟导出对象.transform! tr3
          end
        end
      }
    end
    def 板材躺平

    end

  end#class
end # module amtf_su
